package com.boco.wmworkflow.wfgeneric.action;

import java.util.*;
import com.boco.wmworkflow.base.*;
import com.boco.webmaster.base.WMAction;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import com.boco.wmworkflow.wfgeneric.bo.WfGenericBO;
//import com.boco.webmaster.base.BoHome;
import com.boco.common.util.io.BufOutputStream;
//import com.boco.webmaster.base.IBoActionContext;
//import com.boco.webmaster.wmworkflow.wfgeneric.dto.AttachFileForm;
import com.boco.webmaster.base.SessionManager;
import com.boco.wmworkflow.workmanager.dto.WorkTaskManagerForm;
//import com.boco.wmworkflow.common.util.db.UserTransaction;
import com.boco.common.util.debug.LogHome;
//import com.boco.wmworkflow.common.dbmodle.WF_PROCESS_OUTLINE;
//import com.boco.webmaster.common.dbmodle.FAULT_NOTE;
import com.boco.wmworkflow.base.WfBasicForm;
import com.boco.wmworkflow.wfgeneric.dto.AttachFileForm;
import com.boco.transnms.common.dto.base.IBoActionContext;
import com.boco.transnms.server.bo.base.BoName;
import com.boco.transnms.client.model.base.IBoCommand;
import com.boco.transnms.client.model.base.BoCmdFactory;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.server.bo.helper.workflow.AttempEngineBOHelper;

public class WfGenericAction extends WMAction{
    private static class ActionName{
        private static final String SHOW_WF_STATE           = "showWfState";
        private static final String SHOW_WF_HISEVENT        = "showWfHisEvent";
        private static final String DELETE_ATTACH_FILE      = "deleteAttachFile";
        private static final String DELETE_ATTACH_FILE_OPT      = "deleteAttachFileOpt";
        private static final String ON_CHANGE_PROCESS = "onChangeProcess";
        private static final String ABORT_PROCESS = "abortProcess";
        private static final String SHOW_ABORT_PROCESS = "showAbortProcess";
        private static final String SEND_MESSAGE= "sendMessage";
        private static final String SHOW_CES_AUDIT_SHEET = "showCesAuditSheet";
    }

    private static class FowardName {
        private static final String WORK_FRAME_JSP = "workFrameJsp";
        private static final String SHOW_AUDIT_SHEET_JSP = "showAuditSheetJsp";
    }

    public WfGenericAction() {
    }

    protected ActionForward actionExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception{
        ActionForward actionFoward = null;
        String actionName = mapping.getParameter();

        if(ActionName.SHOW_WF_STATE.equals(actionName)){
            actionFoward = showWfState(mapping, (WfBasicForm) form, request, response);
        }else if(ActionName.SHOW_WF_HISEVENT.equals(actionName)){
            actionFoward = showHisEvent(mapping, (WfBasicForm) form, request, response);
        }else if (ActionName.ON_CHANGE_PROCESS.equals(actionName)) {
            actionFoward = onChangeProcess(mapping, request, (WorkTaskManagerForm) form);
        }else if(ActionName.DELETE_ATTACH_FILE.equals(actionName)){
            actionFoward = delAttachfile(mapping, (AttachFileForm) form, request, response);
        }else if(ActionName.DELETE_ATTACH_FILE_OPT.equals(actionName)){
            actionFoward = delAttachfileOpt(mapping, (AttachFileForm) form, request, response);
        }
        return actionFoward;
    }

    private ActionForward onChangeProcess(ActionMapping actionMapping,
                                  HttpServletRequest request,
                                  WorkTaskManagerForm form)
        throws Exception {
        return actionMapping.findForward(FowardName.WORK_FRAME_JSP);
    }

    private ActionForward abortProcess(ActionMapping actionMapping,
                                  HttpServletRequest request,
                                  WorkTaskManagerForm form)
        throws Exception {
        return actionMapping.findForward(FowardName.WORK_FRAME_JSP);
    }


    private ActionForward showWfState(ActionMapping mapping, WfBasicForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception{
        BufOutputStream out = new BufOutputStream();
        IBoCommand cmd = BoCmdFactory.getInstance().createBoCmd(AttempEngineBOHelper.ActionName.getProcessGraph, new Object[] {new BoActionContext(),form.getProcessKey() });
        byte[] graphBytes = (byte[])cmd.exec();
        out.write(graphBytes);
        SessionManager.getInstance().saveQueryAttr(request,"imageByte",out.getBuf());
        response.sendRedirect(request.getContextPath()+"/wmworkflow/workmanager/workGraph.jsp");
        return null;
    }

    private ActionForward showHisEvent(ActionMapping mapping, WfBasicForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception{
        return mapping.findForward("succeed");
    }
    private ActionForward delAttachfile(ActionMapping mapping, AttachFileForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception{
        IBoActionContext dbContext = WfActionHelper.getBoDbActionContext(request);
        getGenericBO().delAttachFile(dbContext, form.getAttachId());
        List attachIds = (List)request.getSession().getAttribute("attachIds");
        for(int i=0; i<attachIds.size(); i++){
            String tmp = (String)attachIds.get(i);
            if(tmp.equals(form.getAttachId())){
                attachIds.remove(i);
            }
        }
        SessionManager.getInstance().saveQueryAttr(request, "attachIds", attachIds);
        return mapping.getInputForward();
    }
    private ActionForward delAttachfileOpt(ActionMapping mapping, AttachFileForm form, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception{
        IBoActionContext dbContext = WfActionHelper.getBoDbActionContext(request);
        getGenericBO().delAttachFile(dbContext, form.getAttachId());
        return mapping.getInputForward();
    }

    private WfGenericBO getGenericBO(){
        WfGenericBO rtn = new WfGenericBO(BoName.WfGenericBO);
        return rtn;
    }
}
